<template>

  <div>
    <Layout>
      <Content class="layout-content">

        <Row>
          <Col span="24">
            <!--            <div id="jsoneditor" ref="jsonEditor" style="width: 100%; height: 600px;"></div>-->
            <ParamMapping :leftData="params.input"
                          :rightData="params.output" ref="mapping"></ParamMapping>
          </Col>
        </Row>
      </Content>
    </Layout>

  </div>


</template>
<script setup>
import {onMounted, ref, toRaw, inject, getCurrentInstance} from "vue";
import api from '../request/apis'
import moment from "moment";
import {Message, Radio, Row, Switch} from 'view-ui-plus'
import {nextTick, debounce, getValue, setValue} from '../utils'
import ParamMapping from "@/components/ParamMapping.vue";

const mapping = ref();

const params = ref({
  input: {
    params: [],
    basicTypeFlag: false,
    isArrayFlag: false,
    isViewBasicFieldFlag: true,
    isViewArrayFlag: false,
    isUpdateColumnFlag: false,
    isMappingColumnFlag: false,
    isAddColumnFlag: false,
    isRemoveColumnFlag: false,
    isActionColumnFlag: false,
    isAddChildrenColumnFlag: false
  },
  output: {
    params: [],
    basicTypeFlag: false,
    isViewArrayFlag: true,
    isViewBasicFieldFlag: true,
    isArrayFlag: false,
    isActionColumnFlag: true,
    isMappingColumnFlag: true,
    isViewAutoMappingFlag: true,
    isViewClearMappingFlag:true,
    jsonEditorFlag: true
  }
})

const getPrevOutput = async (data) => {
  if (!data) {
    return
  }
  //获取上游的输出
  const versionId = data.versionId;

  const resource = data.resource;
  if (!resource) {
    return;
  }
  const read = await api.getCurrentInputDefine({
    id: versionId,
    processId: null,
    processVersion: data.processVersion,
    resource: resource,
    stepCode: data.stepCode
  });
  return read;
}
const processInfo = ref({});
// get json
onMounted(() => {

  setValue(function () {
    return {
      ...params.value
    };

  });

  getValue(async function (d) {
    if (!d) {
      return
    }

    // console.log(d)
    // if (d.input) {
    //   const db = mapping.value.repairJson(d.input);
    //   params.value.input = db;
    // }
    if (d.output) {
      const dbc = mapping.value.repairJson(d.output);
      params.value.output.params = dbc.params;
      params.value.output.isArrayFlag = dbc.isArrayFlag;
      params.value.output.basicTypeFlag = dbc.basicTypeFlag;
      params.value.output.basicFieldType = dbc.basicFieldType;
    }
    processInfo.value.versionId = d.versionId;
    processInfo.value.resource = d.resource;
    processInfo.value.processVersion = d.processVersion;
    processInfo.value.stepCode = d.stepCode;

    //从上游获取输出
    const dbb = await getPrevOutput(processInfo.value);
    if (dbb) {
      const dbc = mapping.value.repairJson(dbb);
      params.value.input.params = dbc.params;
      params.value.input.isArrayFlag = dbc.isArrayFlag;
      params.value.input.basicTypeFlag = dbc.basicTypeFlag;
      params.value.input.basicFieldType = dbc.basicFieldType;
    }
  });


});

</script>
<style scoped>
</style>